package com.mew.lobby.handler.user;

import com.mew.common.dc.redis.UserProfileRedisService;
import com.mew.common.entity.user.User;
import com.mew.common.exception.ResponseCode;
import com.mew.common.util.StringUtils;
import com.mew.common.rest.bo.MewSession;
import com.mew.lobby.annotation.LobbyHandler;
import com.mew.lobby.handler.sms.SMSValidService;
import com.mew.lobby.message.request.BindTelephoneRequestMessage;
import com.mew.lobby.net.LobbyHandlerMapCmdType;
import com.mew.lobby.net.MewHandler;
import com.mew.lobby.provide.UserFeignService;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@LobbyHandler(LobbyHandlerMapCmdType.Telephone)
public class TelephoneHandler implements MewHandler<BindTelephoneRequestMessage> {

    @Autowired
    private UserProfileRedisService userProfileRedisService;
    @Autowired
    private SMSValidService smsValidService;
    @Autowired
    private UserFeignService userFeignService;

    @Override
    public void handle(ZebraCommand<BindTelephoneRequestMessage> zebraCommand, MewSession session) {
        BindTelephoneRequestMessage message = zebraCommand.getRequest();

        if (null == message|| StringUtils.isEmpty(message.getTelephone())){
            zebraCommand.sendFailureCode(ResponseCode.ParamInvalid.code());
            return;
        }

        User user = userProfileRedisService.getRedisUserById(session.uid);

        if (null==user){
            zebraCommand.sendFailureCode(ResponseCode.LoginUn.code());
            return;
        }

        //验证验证码
        int code = smsValidService.authCode(message.getTelephone(), message.getAuthCode());
        if (code!= ResponseCode.Success.code()){
            zebraCommand.sendFailureCode(code);
            return;
        }
        zebraCommand.sendFailureCode(userFeignService.bindTelephone(session.uid,message.getTelephone()));
    }


}
